package com.lwb.ztx.news.widget;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lwb.ztx.R;
import com.lwb.ztx.api.Api;
import com.lwb.ztx.bean.NewsBean;
import com.lwb.ztx.bean.NewsTopicBean;
import com.lwb.ztx.news.presenter.NewsTopicPresenter;
import com.lwb.ztx.news.presenter.NewsTopicPresenterImpl;
import com.lwb.ztx.news.view.NewsTopicView;
import com.lwb.ztx.utils.ActivitySkipUtils;
import com.lwb.ztx.utils.LogUtils;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class NewsTopicActivity extends SwipeBackActivity implements NewsTopicView,
        NewsTopicAdapter.OnTopicSliderClickListener, XRecyclerView.LoadingListener {
    private NewsBean newsBean;
    private NewsTopicPresenter presenter;
    @InjectView(R.id.news_topic_recyclerview)
    XRecyclerView recyclerView;
    NewsTopicAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_topic);
        ButterKnife.inject(this);
        presenter = new NewsTopicPresenterImpl(this);
        initIntent();
        String url = newsBean.getLinkUrl();
        if(url.contains("?")) {
            url = url + Api.END;
        }
        presenter.getTopicBeans(url);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setLoadingListener(this);
        recyclerView.setLoadingMoreEnabled(false);
        adapter = new NewsTopicAdapter(this, this);
        recyclerView.setAdapter(adapter);
    }

    private void initIntent() {
        newsBean = (NewsBean) getIntent().getSerializableExtra("news");
        LogUtils.i(newsBean.toString());
    }

    @Override
    public void setTopic(List<NewsTopicBean> beans) {
        LogUtils.d("Result:" + beans.toString());
        recyclerView.refreshComplete();
        adapter.setBeans(beans);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void OnTopicSliderClick(NewsBean bean) {
        LogUtils.d(bean.toString());
        ActivitySkipUtils.readNews(this, bean);
    }

    @Override
    public void onRefresh() {
        presenter.getTopicBeans(newsBean.getLinkUrl());
    }

    @Override
    public void onLoadMore() {

    }
}
